package com.lsh.oms.core.service.tms.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.enums.OrderOperateType;
import com.lsh.oms.core.enums.PayChannel;
import com.lsh.oms.core.enums.ShippingReceiptStatus;
import com.lsh.oms.core.enums.ShippingStatus;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.model.order.OrderShippingHead;
import com.lsh.oms.core.model.order.OrderSignHead;
import com.lsh.oms.core.service.order.OrderUpdateService;
import com.lsh.oms.core.service.other.ShippingOrderService;
import com.lsh.oms.core.service.other.SignOrderService;
import com.lsh.oms.core.service.tms.TmsService;
import com.lsh.oms.core.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 2020-01-02
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */

@Service
@Slf4j
public class TmsServiceImpl implements TmsService {

    @Autowired
    private OrderUpdateService orderUpdateService;

    @Autowired
    private ShippingOrderService shippingOrderService;

    @Autowired
    private SignOrderService signOrderService;


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean dealDelivery(OrderHead orderHead, OrderShippingHead shippingHead) {

        OrderHead entity = new OrderHead();
        boolean resultUpdate = this.orderUpdateService.updateOrderStatus(orderHead.getOrderCode(), entity, OrderOperateType.ORDER_DELIVERY, orderHead.getVenderId()) >= 1;

        if (resultUpdate) {

            OrderShippingHead orderShippingHead = this.shippingOrderService.selectOneByOrderCode(orderHead.getOrderCode(), false);

            shippingHead = new OrderShippingHead();
            shippingHead.setShippingOrderId(orderShippingHead.getShippingOrderId());
            shippingHead.setStatus(ShippingStatus.DELIVERING.getIndex());
            shippingHead.setReceiptStatus(ShippingReceiptStatus.UN_RECEIPT.getIndex());
            shippingHead.setActivedAt(DateUtil.getCurrentTime());
            shippingHead.setUpdatedAt(DateUtil.getCurrentTime());

            resultUpdate = shippingOrderService.updateStatus(shippingHead);
        }

        return resultUpdate;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean dealSign(OrderHead orderHead, OrderShippingHead shippingHead) {

        boolean resultUpdate = this.orderUpdateService.updateOrderStatus(orderHead.getOrderCode(), null, OrderOperateType.ORDER_ARRIVED, orderHead.getVenderId()) == 1;
        if (resultUpdate) {
            OrderShippingHead orderShippingHead = this.shippingOrderService.selectOneByOrderCode(orderHead.getOrderCode(), false);

            shippingHead = new OrderShippingHead();
            shippingHead.setShippingOrderId(orderShippingHead.getShippingOrderId());
            shippingHead.setStatus(ShippingStatus.DELIVERED.getIndex());
            shippingHead.setReceiptStatus(ShippingReceiptStatus.RECEIPT.getIndex());
            shippingHead.setArrivedAt(DateUtil.getCurrentTime());
            shippingHead.setActivedAt(DateUtil.getCurrentTime());
            shippingHead.setUpdatedAt(DateUtil.getCurrentTime());

            shippingOrderService.updateStatus(shippingHead);

            OrderSignHead orderSignHead = signOrderService.getSignHeadByOrderId(orderHead.getOrderCode(),false);
            if(null == orderSignHead){
                return true;
            }
            Integer payType = orderSignHead.getPayType();
            if (null != payType && payType == PayChannel.MONEY.getIndex()) {

                BigDecimal money = orderSignHead.getMoney().setScale(0, BigDecimal.ROUND_DOWN);

                OrderSignHead signHead = new OrderSignHead();
                signHead.setReceiptOrderId(orderSignHead.getReceiptOrderId());
                JSONObject ext = JSON.parseObject(signHead.getExt());
                ext.put("money", money);

                signOrderService.updateExtByReceiptOrderId(signHead);
            }

        }

        return true;
    }
}
